/**角色
 * author:noodles
 * 定义clientRole、ControlRole、NPCMRole等角色的公共方法
 * */
package com.gamebase.role
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	
	public class Role extends Sprite
	{
		private var frontBM:Bitmap;//前景在角色中所截位图（场景里的遮挡物件图）
		private var frontBMD:BitmapData;
		private var frontBMDRec:Rectangle;//截图区域
		private var frontRect:Rectangle;//截图区域在场景中的映射区域
		private var zeroPoint:Point = new Point(0,0);//注册点
		
		public function Role()
		{
			super();
		}
		
		/**初始化前景截图设置
		 * bm:场景前景位图
		 * inRolePoint 截图添加到角色中的坐标点
		 * */
		protected function resetFront(frontRectangle:Rectangle,frontBMOffset:Point):void
		{
			if(frontBMD != null)
			{
				this.removeChild(frontBM);
				frontBMD.dispose();
				frontBMD = null;
				frontRect = null;
				frontBM = null;
			}
			frontRect = frontRectangle;
			frontBMD = new BitmapData(frontRect.width,frontRect.height,true,0);
			frontBM = new Bitmap(frontBMD);
			frontBM.x = int(frontBMOffset.x);
			frontBM.y = int(frontBMOffset.y);
			frontBMDRec = new Rectangle(0,0,frontBMD.width,frontBMD.height);
			this.addChild(frontBM);
			frontBM.alpha = 0.8;
		}
		
		public function drawFront(bmd:BitmapData/*,globalP:Point=null*/):void
		{
			if(frontRect == null)
				return;
			
			//var point:Point = frontBM.localToGlobal(new Point(0,0));
			frontRect.x = int(this.x) + int(frontBM.x);//point.x - globalP.x;//
			frontRect.y = int(this.y) + int(frontBM.y);//point.y - globalP.y;//
			frontBMD.copyPixels(bmd,frontRect,zeroPoint)
		}
		
		public function clearFront():void
		{
			if(frontBMD == null)
				return;
			frontBMD.fillRect(frontBMDRec,0);
		}
		
		protected function destroyRole():void
		{
			if(frontBM != null)
			{
				if(frontBM.parent != null)
					frontBM.parent.removeChild(frontBM);
					
				frontBMD.dispose();
				frontBMD = null;
				frontRect = null;
				frontBM = null;
			}
			zeroPoint = null;
		}
		
		public function set enabled(value:Boolean):void
		{
			this.mouseEnabled = value;
		}

	}
}